<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>10.splice与slice实现数组的增删改查</title>
</head>

<body>
  <script>
    let arr = [0, 1, 2, 3, 4, 5, 6];
    console.log(arr.slice(1, 3)); // [1,2]    
    console.log(arr.slice(1)); // [1,2,3,4,5,6]    
    //使用 slice 方法从数组中截取部分元素组合成新数组（并不会改变原数组），不传第二个参数时截取到数组的最后元素,不设置参数是为获取所有元素
    console.log(arr.slice());//[0, 1, 2, 3, 4, 5, 6]

    //splice 增删，替换元素，还会改变原数组

    let arr2 = [0, 1, 2, 3, 4, 5];
    // 从0移除，移除3个
    //let hd= arr2.splice(0∑3);// arr2 ：(3) [3, 4, 5]；hd: (3) [0, 1, 2]
    // 从1移除，移除1个
    //let hd = arr2.splice(1,1);//arr2 ：(5)[0, 2, 3, 4, 5] ； hd： [1]
    // 从1移除，第三个参数代表增加元素，所以相当于替换1个
    // let hd = arr2.splice(1, 1,"houdunren");//arr2:[0, 'houdunren', 2, 3, 4, 5];hd:[1]
    // arr2 ['hdcms', 'houdunren', 3, 4, 5] //hd: [0, 1, 2]
    // 追加，从1开始，不替换，追加
    //let hd = arr2.splice(1, 0, "houdunren");//arr2:[0, 'houdunren',1, 2, 3, 4, 5];hd:[]
    //let hd = arr2.splice(1, 0, "houdunren","aa","bb");//arr2:(9) [0, 'houdunren', 'aa', 'bb', 1, 2, 3, 4, 5]; hd:[]
    //向末尾增加
    //let hd = arr2.splice(arr2.length,0,"houdunren");//arr2:[0, 1, 2, 3, 4, 5, 'houdunren'];hd:[]
    //头部增加
    let hd = arr2.splice(0,0,'houdunren');//arr2:(7) ['houdunren', 0, 1, 2, 3, 4, 5]；hd：[]



    // 头部增加

    console.log(arr2);
    console.log(hd);
  </script>
</body>

</html>